import { defineStore } from "pinia";
import { ref, reactive } from "vue"
import { ElMessage } from "element-plus";
import { useEnumStore } from "@/store/enumeration"
import { orderEquityPackageDetailPagePost } from "@/api/equity"
import mittBus from '@/utils/mitt';

import useInstance from '@/hooks/useInstance';
export const orderEquityPackageDetailStore = defineStore("orderEquityPackageDetailStoreId", () => {
    const { global } = useInstance();
    const { receiveStatus, grantStatus, redeemStatus, grantType } = useEnumStore()
    const addAndEditRef = ref<{ show: (title: string, row?: any) => void }>()
    const detailRef = ref<{ show: (title: string, row?: any) => void }>()

    const detailData = ref([
        {
            type: "text",
            prop: "channelKey",
            name: "渠道Key",
            value: null
        },
    ])
    const tableProp = reactive(
        {
            name: "orderEquityPackageDetail",
            propsList: [

                {
                    prop: 'orderNo',
                    label: '订单号',
                    type: 'normal',
                    minWidth: '220px',
                    hidden: true,
                },
                // {
                //     prop: 'equityCode',
                //     label: '权益编码',
                //     type: 'normal',
                //     minWidth: '180px',
                //     hidden: true,
                // },
                {
                    prop: 'equityName',
                    label: '权益名称',
                    type: 'normal',
                    minWidth: '180px',
                    hidden: true,
                },
                {
                    prop: 'equityGroup',
                    label: '立减金顺序',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'annualPayNum',
                    label: '年度缴次',
                    type: 'normal',
                    minWidth: '70px',
                    hidden: true,
                },
                {
                    prop: 'mobile',
                    label: '手机号',
                    type: 'normal',
                    minWidth: '110px',
                    hidden: true,
                },
                {
                    prop: 'equityPackageDetailNo',
                    label: '权益包明细单号',
                    type: 'normal',
                    minWidth: '240px',
                    hidden: true,
                },
                // {
                //     prop: 'packageCode',
                //     label: '权益包编码',
                //     type: 'normal',
                //     minWidth: '90px',
                //     hidden: true,
                // },
                // {
                //     prop: 'portfolioCode',
                //     label: '权益组合编码',
                //     type: 'normal',
                //     minWidth: '100px',
                //     hidden: true,
                // },
       
                {
                    prop: 'effectiveTime',
                    label: '生效时间',
                    type: 'normal',
                    minWidth: '160px',
                    hidden: true,
                },
                {
                    prop: 'expireTime',
                    label: '失效时间',
                    type: 'normal',
                    minWidth: '160px',
                    hidden: true,
                },
                {
                    prop: 'channelKey',
                    label: '渠道 key',
                    type: 'normal',
                    minWidth: '70px',
                    hidden: true,
                },
                // {
                //     prop: 'productCategory',
                //     label: '产品大类',
                //     type: 'normal',
                //     minWidth: '140px',
                //     hidden: true,
                // },
                // {
                //     prop: 'receiveType',
                //     label: '领取方式',
                //     type: 'normal',
                //     minWidth: '70px',
                //     hidden: true,
                // },
                // {
                //     prop: 'receiveValue',
                //     label: '领取方式值',
                //     type: 'normal',
                //     minWidth: '140px',
                //     hidden: true,
                // },
                {
                    prop: 'receiveStatus',
                    label: '领取状态',
                    type: 'normal',
                    minWidth: '70px',
                    hidden: true,
                },
                {
                    prop: 'receiveTime',
                    label: '领取时间',
                    type: 'normal',
                    minWidth: '160px',
                    hidden: true,
                },
                {
                    prop: 'grantType',
                    label: '发放方式',
                    type: 'normal',
                    minWidth: '140px',
                    hidden: true,
                },
                {
                    prop: 'grantStatus',
                    label: '发放状态',
                    type: 'normal',
                    minWidth: '80px',
                    hidden: true,
                },
                {
                    prop: 'grantTime',
                    label: '发放时间',
                    type: 'normal',
                    minWidth: '160px',
                    hidden: true,
                },
                // {
                //     prop: 'grantAccount',
                //     label: '发放账户',
                //     type: 'normal',
                //     minWidth: '250px',
                //     hidden: true,
                // },
                {
                    prop: 'redeemStatus',
                    label: '核销状态',
                    type: 'normal',
                    minWidth: '100px',
                    hidden: true,
                },
                {
                    prop: 'redeemTime',
                    label: '核销时间',
                    type: 'normal',
                    minWidth: '160px',
                    hidden: true,
                },
                {
                    prop: 'redeemOrderNo',
                    label: '核销订单号',
                    type: 'normal',
                    minWidth: '140px',
                    hidden: true,
                },
                {
                    prop: 'paymentId',
                    label: '支付ID',
                    type: 'normal',
                    minWidth: '140px',
                    hidden: true,
                },
                {
                    prop: 'signId',
                    label: '签约ID',
                    type: 'normal',
                    minWidth: '140px',
                    hidden: true,
                },

                {
                    prop: 'usageTime',
                    label: '使用时间',
                    type: 'normal',
                    minWidth: '160px',
                    hidden: true,
                },
                // {
                //     prop: 'extInfo',
                //     label: '扩展信息',
                //     type: 'normal',
                //     minWidth: '200px',
                //     hidden: true,
                // },

            ]
        })

    const listSearch = reactive({
        orders: ["created_time desc"],
        condition: {
            mobile: null,//手机号
            productCategory: null,//产品
            equityName: null,// 权益名称
            channelKey: null,//渠道ID
            packageConfigType: null,//环境类型
            
            minusMoney: null,// 立减金额
            receiveAccount: null,//领取账号
            receiveStatus: null,//领取状态
            grantStatus: null,// 发放状态
            redeemStatus: null,//核销状态

            paymentId: null,//支付ID
            signId: null,//签约ID
            reductionOrder: null,//立减金顺序
            orderNo: null,//订单号 
            redeemOrderNo: null,// 核销订单号

            createdTimeStart: null,//创建时间 开始 1
            createdTimeEnd: null,//创建时间 结束

            effectiveTimeStart: null,// 生效时间开始 2
            effectiveTimeEnd: null,// 生效时间开始


            expireTimeStart: null,//失效时间 3
            expireTimeEnd: null,//失效时间

            usageTimeStart: null,// 使用时间 4
            usageTimeEnd: null,// 使用时间

            redeemTimeStart: null,// 核销时间 5
            redeemTimeEnd: null,// 核销时间

            receiveTimeStart: null,//领取时间 6
            receiveTimeEnd: null,//领取时间
        },
        pageSize: 20,
        pageNum: 1
    });
    const pageParams = reactive({
        tableHeight: 400,
        total: 0,
        loadIng: false,
        tableData: []

    })
    const getDate1 = async (type: string) => {
        pageParams.loadIng = true
        if (type == 'reset') {
            listSearch.pageNum = 1
            listSearch.pageSize = 20
            Object.keys(listSearch.condition).forEach((it: any) => {
                // @ts-ignore
                listSearch.condition[it] = null
            })
        }

        try {
            const res = await orderEquityPackageDetailPagePost(listSearch)
            pageParams.loadIng = false
            if (res.code == 20000) {
                pageParams.tableData = res.data.records && res.data.records.map((item: any) => {
                    return {
                        ...item,
                        receiveStatus: receiveStatus[item.receiveStatus - 1]?.value, //领取状态
                        grantStatus: grantStatus[item.grantStatus - 1]?.value,//发放状态
                        redeemStatus: redeemStatus[item.redeemStatus - 1]?.value,//核销状态
                        grantType: grantType[item.grantType - 1]?.value//发放方式
                    }
                })
                pageParams.total = res.data.total
            }
        } catch (error) {
            console.log(error);
            pageParams.loadIng = false

        }finally{
            mittBus.emit('scrollUpdate')
        }
    }
    const detailBtn = async (row: any) => {
        try {
            const res = await orderEquityPackageDetailPagePost({ id: row.id })
            detailRef.value?.show('edit', res.data)
        } catch (error) {
            console.log(error);
        }
    }
    const handPage = (params: any) => {
        listSearch.pageNum = params.pageNum
        listSearch.pageSize = params.pageSize
        getDate1('search')
    }

    return {
        detailData, tableProp, listSearch, pageParams, addAndEditRef,
        getDate1, detailBtn, handPage,
    }
})

/* 

, {
    persist: {
        // storage: sessionStorage,
        // paths: ['detailData', 'tableProp'],
    }
}
*/